#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_WriteDataById_0x2E_test.py
@Time    :   2024/09/10 13:31:28
@Author  :   yangliang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from urllib import response
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True


WriteDID = [([0x2E, 0xF1, 0x90, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfE], [0x6E, 0xF1, 0x90]),
            # ([0x2E,0xF1,0x84,0x22,0x09,0x30,0x00,0x00,0x00,0x00,0x00,0x01],[0x6E,0xF1,0x84]),
            ([0x2E, 0x12, 0x66, 0x00, 0x00, 0x00, 0x01], [0x6E, 0x12, 0x66]),
            ([0x2E, 0x12, 0x6A, 0x00, 0x01], [0x6E, 0x12, 0x6A]),
            ([0x2E, 0x12, 0x6B, 0x00, 0x01], [0x6E, 0x12, 0x6B]),
            ([0x2E, 0x12, 0x6C, 0x00, 0x01], [0x6E, 0x12, 0x6C])]

WriteDID_lock = [   ([0x2E, 0xF1, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03], [0x6E, 0xF1, 0x87]),
                    ([0x2E, 0xF1, 0x83, 0x20, 0x22, 0x09, 0x30, 0x00, 0x00, 0x00], [0x6E, 0xF1, 0x83]),
                    ([0x2E, 0xF1, 0x8A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], [0x6E, 0xF1, 0x8A]),
                    ([0x2E, 0xF1, 0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], [0x6E, 0xF1, 0x91]),
                    ([0x2E, 0xF1, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], [0x6E, 0xF1, 0x95]),
                    ([0x2E, 0xF1, 0x8B, 0x20, 0x22, 0x09, 0x30],[0x6E, 0xF1, 0x8B]),  # ECU生产日期
                    ([0x2E, 0xF1, 0x8C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], [0x6E, 0xF1, 0x8C]),  # ECU序列号
                    ([0x2E, 0xF1, 0x99, 0x20, 0x22, 0x09, 0x30],[0x6E, 0xF1, 0x99]),  # 编程日期
                    # ([0x2E,0x12,0x60,0x00,0x00],[0x6E,0x12,0x60]),  # ECU电源电压
                    ([0x2E, 0x12, 0x61, 0x00, 0x00], [0x6E, 0x12, 0x61]),  # 车速
                    ([0x2E, 0x12, 0x62, 0x00], [0x6E, 0x12, 0x62]),  # 点火状态
                    ([0x2E, 0x12, 0x63, 0x00], [0x6E, 0x12, 0x63]),  # 整车档位
                    ([0x2E, 0x12, 0x64, 0x00, 0x00], [0x6E, 0x12, 0x64]),  # 雷达SoC温度
                    ([0x2E, 0x12, 0x65, 0x00], [0x6E, 0x12, 0x65]),
                    # ([0x2E,0x12,0x66,0x00,0x00,0x00,0x00],[0x6E,0x12,0x66]),  # 目标物坐标信息
                    ([0x2E, 0x12, 0x68, 0x00, 0x00], [0x6E, 0x12, 0x68]),  # 水平角度误差
                    ([0x2E, 0x12, 0x69, 0x00], [0x6E, 0x12, 0x69]),  # 校准错误信息
                    # ([0x2E,0x12,0x6A,0x00,0x00],[0x6E,0x12,0x6A]),  # 左右雷达传感器中心距离
                    # ([0x2E,0x12,0x6B,0x00,0x00],[0x6E,0x12,0x6B]),  # 前后雷达传感器中心距离
                    # ([0x2E,0x12,0x6C,0x00,0x00],[0x6E,0x12,0x6C])  # 车辆轴距
                 ]

# WriteDID = [([0x2E,0xF1,0x90,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfE],[0x6E,0xF1,0x90]),
#             # ([0x2E,0xF1,0x84,0x22,0x09,0x30,0x00,0x00,0x00,0x00,0x00,0x01],[0x6E,0xF1,0x84]),
#             ([0x2E,0x12,0x66,0x00,0x00,0x00,0x01],[0x6E,0x12,0x66]),
#             ([0x2E,0x12,0x6A,0x00,0x01],[0x6E,0x12,0x6A]),
#             ([0x2E,0x12,0x6B,0x00,0x01],[0x6E,0x12,0x6B]),
#             ([0x2E,0x12,0x6C,0x00,0x01],[0x6E,0x12,0x6C])]


WriteDID_NRC = [    ([0x2E], [0x7F, 0x2E, 0x13]),  # NRC13
                    ([0x2E, 0xF1], [0x7F, 0x2E, 0x13]),
                    ([0x2E, 0x12], [0x7F, 0x2E, 0x13]),
                    ([0x2E, 0xF1, 0x90, 0xff, 0x15, 0x11, 0x01], [0x7F, 0x2E, 0x13]),
                    ([0x2E, 0xF1, 0x84, 0x28, 0xFF, 0x01], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0x12, 0x66], [0x7F, 0x2E, 0x13]),
                    ([0x2E, 0x12, 0x6A, 0xff, 0x15, 0x11, 0x01], [0x7F, 0x2E, 0x13]),
                    ([0x2E, 0x12, 0x6B, 0x28, 0xFF, 0x01], [0x7F, 0x2E, 0x13]),
                    ([0x2E, 0x12, 0x6C, 0x55, 0xFF, 0x01], [0x7F, 0x2E, 0x13]),
                    ([0x2E, 0x3F, 0x87, 0x46, 0x30, 0x32, 0x2D,0x33, 0x36, 0x30, 0x32], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0xF1, 0x87, 0x46, 0x30, 0x32, 0x2D,0x33, 0x36, 0x30, 0x32], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0xF1, 0x83, 0x00, 0x00, 0x00, 0x20, 0x22,0x09, 0x30, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0xF1, 0x8A, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0xF1, 0x91, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0xF1, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0xF1, 0x8B, 0x00, 0x00, 0x00, 0x20,0x22, 0x09, 0x30], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0xF1, 0x8C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0xF1, 0x99, 0x00, 0x00, 0x00, 0x20,0x22, 0x09, 0x30], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0x12, 0x60, 0x00, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0x12, 0x61, 0x00, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0x12, 0x62, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0x12, 0x63, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0x12, 0x64, 0x00, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0x12, 0x65, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0x12, 0x68, 0x00, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0x12, 0x69, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31]),
                    ([0x2E, 0xF1, 0x84, 0x00, 0x00, 0x00, 0x22, 0x09, 0x30, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x31])
                ]


class Test_WriteDataById_Phy(object):
    ids = ["Request:{0}".format(binascii.b2a_hex(bytearray(req[1:3]), ' ')) for req, resp in WriteDID]
    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.description('Test DID of 0x2E that need unlock in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req, resp', WriteDID, ids=ids)
    def test_WriteDataById_with_unlock_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22]+req[1:3], [0x62], 0, iStep, False)
        if uds_resp[3:] != req[3:]:
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[3:]), ' ')
            cor_RespStr = binascii.b2a_hex(bytearray(req[3:]), ' ')
            logging.error(f'correct response:{cor_RespStr}, actual response:[{actuRespStr}]')
        with assume:assert (uds_resp[0] == 0x62 and uds_resp[3:] == req[3:])


    ids = ["Request:{0}".format(binascii.b2a_hex(bytearray(req), ' ')) for req, resp in WriteDID_NRC]
    @allure.severity('minor')
    @allure.description('Test NRC of 0x2E in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', WriteDID_NRC, ids=ids)
    def test_WriteDataById_NRC_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)

    @allure.severity('minor')
    @allure.description('Test NRC priority of 0x2E in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_WriteDataById_NRC_priority_Extended_Phy(self, extend_session, action, Driver):
        # 对2E服务的安全解锁检查不通过，报NRC33，无法对DID解锁不通过做check
        with allure.step('step1:Request:0x2E,0x12,0x6A,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00, # Response:0x7F,0x2E,0x33'):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x2E, 0x12, 0x6A, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00], 12)
            if uds_rep[0:3] != [0x7F, 0x2E, 0x33]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(f'correct response:[0x7F,0x2E,0x33], actual response:[{actuRespStr}]')
            with assume: assert uds_rep[0:3] == [0x7F, 0x2E, 0x33]
        with allure.step('step2:Request:0x2E, # Response:0x7F,0x2E,0x33'):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x2E], 1)
            if uds_rep[0:3] != [0x7F, 0x2E, 0x33]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(f'correct response:[0x7F,0x2E,0x33], actual response:[{actuRespStr}]')
            with assume: assert uds_rep[0:3] == [0x7F, 0x2E, 0x33]
        with allure.step('step3:Request:0x2E,0x3A,0x01,0x00, # Response:0x7F,0x2E,0x31'):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x2E, 0x3A, 0x01, 0x00], 4)
            if uds_rep[0:3] != [0x7F, 0x2E, 0x33]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(f'correct response:[0x7F,0x2E,0x33], actual response:[{actuRespStr}]')
            with assume: assert uds_rep[0:3] == [0x7F, 0x2E, 0x33]
        with allure.step('step4:Request:0x10,0x01, # Response:0x50,0x01'):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x01], 2)
            if uds_rep[0:6] != [0x50, 0x01, 0x00, 0x32, 0x00, 0xC8]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(f'correct response:[0x50,0x01,0x00,0x32,0x00,0xC8], actual response:[{actuRespStr}]')
            with assume: assert uds_rep[0:6] == [0x50, 0x01, 0x00, 0x32, 0x00, 0xC8]
        with allure.step('step5:Request:0x2E,0x12,0x6A,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00, # Response:0x7F,0x2E,0x7F'):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x2E, 0x12, 0x6A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00], 12)
            if uds_rep[0:3] != [0x7F, 0x2E, 0x7F]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')
                logging.error(f'correct response:[0x7F,0x2E,0x7F], actual response:[{actuRespStr}]')
            with assume: assert uds_rep[0:3] == [0x7F, 0x2E, 0x7F]

    @pytest.mark.smoke
    @allure.severity('minor')
    @allure.description('Test DID of 0x2E in extended session with physical addressing after reset')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_WriteDataById_value_after_reset_Extended_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x2E, 0x12, 0x66, 0x00, 0x03, 0x00, 0x00], [0x6E, 0x12, 0x66], 3, iStep, True)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0x12, 0x66], [ 0x62, 0x12, 0x66, 0x00, 0x03, 0x00, 0x00], 0, iStep, False)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x2E, 0x12, 0x6A, 0x00, 0x03], [0x6E, 0x12, 0x6A], 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0x12, 0x6A], [0x62, 0x12, 0x6A, 0x00, 0x03], 0, iStep, False)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x2E, 0x12, 0x6B, 0x01, 0x04], [0x6E, 0x12, 0x6B], 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0x12, 0x6B], [0x62, 0x12, 0x6B, 0x01, 0x04], 0, iStep, False)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x2E, 0x12, 0x6C, 0x02, 0x05], [0x6E, 0x12, 0x6C], 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0x12, 0x6C], [0x62, 0x12, 0x6C, 0x02, 0x05], 0, iStep, False)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x2E, 0xF1, 0x90, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc], [0x6E, 0xF1, 0x90], 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0xF1, 0x90], [0x62, 0xF1, 0x90, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc], 0, iStep, False)

        iStep = Driver['CAN'].restartMRR_11(Driver, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0x12, 0x66], [0x62, 0x12, 0x66, 0x00, 0x03, 0x00, 0x00], 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0x12, 0x6A], [0x62, 0x12, 0x6A, 0x00, 0x03], 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0x12, 0x6B], [0x62, 0x12, 0x6B, 0x01, 0x04], 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0x12, 0x6C], [0x62, 0x12, 0x6C, 0x02, 0x05], 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22, 0xF1, 0x90], [0x62, 0xF1, 0x90, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc], 0, iStep, False)

    ids = ["Request:{0}".format(binascii.b2a_hex(bytearray(req[1:3]), ' ')) for req, resp in WriteDID]
    @allure.severity('minor')
    @pytest.mark.repeat(1)
    @allure.description('Test DID all value set to 0 of 0x2E with unlock in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req, resp', WriteDID, ids=ids)
    def test_WriteDataById_all_value_set0_with_unlock_then_reset_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        req0 = req[0:3] + ([0x00]*(len(req)-3))
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req0, resp, 3, iStep, True)
        iStep = Driver['CAN'].restartMRR_Power(Driver, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22]+req[1:3], [0x62], 0, iStep, False)
        if set(uds_resp[3:]) != {0x00}:
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[3:]), ' ')
            logging.error(f'correct response:all byte is {0x00}, actual response:[{actuRespStr}]')
        with assume:assert set(uds_resp[3:]) == {0x00}

    ids = ["Request:{0}".format(binascii.b2a_hex(bytearray(req[1:3]), ' ')) for req, resp in WriteDID]
    @pytest.mark.repeat(1)
    @allure.severity('minor')
    @allure.description('Test DID all value set to 1 of 0x2E with unlock in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req, resp', WriteDID, ids=ids)
    def test_WriteDataById_all_value_set1_with_unlock_then_reset_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        req0 = req[0:3] + ([0x01]*(len(req)-3))
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req0, resp, 3, iStep, True)
        iStep = Driver['CAN'].restartMRR_Power(Driver, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x22]+req[1:3], [0x62], 0, iStep, False)
        if set(uds_resp[3:]) != {0x00}:
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[3:]), ' ')
            logging.error(f'correct response:all byte is {0x00}, actual response:[{actuRespStr}]')
        with assume:
            assert set(uds_resp[3:]) == {0x01}
    
    ids = ["Request:{0}".format(binascii.b2a_hex(bytearray(req[1:3]), ' ')) for req, resp in WriteDID]
    @allure.severity('normal')
    @allure.story('Write data by identifier test')
    @allure.description('Test NRC7F of 0x2E in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req, resp', WriteDID, ids=ids)
    def test_WriteDataById_NRC7F_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '7F 2E 7F', 1, iStep, False)